package com.findyou.controller;

import com.findyou.pojo.Article;
import com.findyou.pojo.PageBean;
import com.findyou.pojo.Result;
import com.findyou.service.ArticleService;
import com.findyou.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Title: ArticleController
 * @Author 贾金博
 * @Package com.findyou.controller
 * @Date 2024/3/9 14:18
 * @description:
 */

@RestController
@RequestMapping("article")
@CrossOrigin // 跨域
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        Result result = articleService.add(article);
        return result;
    }

    @PutMapping
    public Result update(@RequestBody Article article) {
        Result result = articleService.update(article);
        return result;
    }

    @GetMapping("detail")
    public Result detail(int id) {
        Result result = articleService.detail(id);
        return result;
    }

    @DeleteMapping
    public Result delete(int id) {
        Result result = articleService.delete(id);
        return result;
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId, // 并不是必须的
            @RequestParam(required = false) String state
    ) {
        PageBean<Article>pageBean = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pageBean);
    }

}
